// Display Library for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
// Requires HW SPI and Adafruit_GFX. Caution: the e-paper panels require 3.3V supply AND data lines!
//
// based on Demo Example from Good Display: http://www.e-paper-display.com/download_detail/downloadsId=808.html
// Panel: 5.65inch ACeP 7-Color E-Paper : https://www.waveshare.com/product/displays/e-paper/5.65inch-e-paper-module-f.htm
// Controller: unknonw
//
// Author: Jean-Marc Zingg
//
// Version: see library.properties
//
// Library: https://github.com/ZinggJM/GxEPD2

#ifndef _GxEPD2_565c_H_
#define _GxEPD2_565c_H_

#include <spi_ram.h>
#include <spiRam.h>
#include "../GxEPD2_EPD.h"

class GxEPD2_565c : public GxEPD2_EPD {
public:
    // attributes
    static const uint16_t WIDTH = 600;
    static const uint16_t HEIGHT = 400;
    static const GxEPD2::Panel panel = GxEPD2::ACeP565;
    static const bool hasColor = true;
    static const bool hasPartialUpdate = true;
    static const bool hasFastPartialUpdate = true;
    static const uint16_t power_on_time = 100; // ms, e.g. 96001us
    static const uint16_t power_off_time = 100; // ms, e.g. 60001us
    static const uint16_t full_refresh_time = 12000; // ms, e.g. 11354001us
    static const uint16_t partial_refresh_time = 12000; // ms, e.g. 11354001us
    // constructor
    GxEPD2_565c(SPIClass *, int8_t cs, int8_t dc, int8_t rst, int8_t busy);

    // methods (virtual)
    //  Support for Bitmaps (Sprites) to Controller Buffer and to Screen
    void clearScreen(uint8_t value = 0xFF) override; // init controller memory and screen (default white)
    void clearScreen(uint8_t black_value, uint8_t color_value); // init controller memory and screen
    void writeScreenBuffer(uint8_t value = 0xFF) override; // init controller memory (default white)
    void writeScreenBuffer(uint8_t black_value, uint8_t color_value); // init controller memory
    // write to controller memory, without screen refresh; x and w should be multiple of 8
    void writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false,
                    bool mirror_y = false, bool pgm = false);

    void writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
                        int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false,
                        bool pgm = false);

    void writeImage(const uint8_t *black, const uint8_t *color, int16_t x, int16_t y, int16_t w, int16_t h,
                    bool invert = false, bool mirror_y = false, bool pgm = false);

    void writeImagePart(const uint8_t *black, const uint8_t *color, int16_t x_part, int16_t y_part, int16_t w_bitmap,
                        int16_t h_bitmap,
                        int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false,
                        bool pgm = false);

    // write sprite of native data to controller memory, without screen refresh; x and w should be multiple of 8
    void writeNative(const uint8_t *data1, const uint8_t *data2, int16_t x, int16_t y, int16_t w, int16_t h,
                     bool invert = false, bool mirror_y = false, bool pgm = false);

    void
    writeNative(spiRam &data1, const uint8_t *data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false,
                bool mirror_y = false, bool pgm = false);

    void writeNativePart(const uint8_t *data1, const uint8_t *data2, int16_t x_part, int16_t y_part, int16_t w_bitmap,
                         int16_t h_bitmap,
                         int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false,
                         bool pgm = false);

    // write to controller memory, with screen refresh; x and w should be multiple of 8
    void drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false,
                   bool mirror_y = false, bool pgm = false);

    void drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
                       int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false,
                       bool pgm = false);

    void drawImage(const uint8_t *black, const uint8_t *color, int16_t x, int16_t y, int16_t w, int16_t h,
                   bool invert = false, bool mirror_y = false, bool pgm = false);

    void drawImagePart(const uint8_t *black, const uint8_t *color, int16_t x_part, int16_t y_part, int16_t w_bitmap,
                       int16_t h_bitmap,
                       int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false,
                       bool pgm = false);

    // write sprite of native data to controller memory, with screen refresh; x and w should be multiple of 8
    void drawNative(const uint8_t *data1, const uint8_t *data2, int16_t x, int16_t y, int16_t w, int16_t h,
                    bool invert = false, bool mirror_y = false, bool pgm = false);

    void
    drawNative(spiRam &data1, const uint8_t *data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false,
               bool mirror_y = false, bool pgm = false);

    void refresh(bool partial_update_mode = false) override; // screen refresh from controller memory to full screen
    void refresh(int16_t x, int16_t y, int16_t w,
                 int16_t h) override; // screen refresh from controller memory, partial screen
    void powerOff() override; // turns off generation of panel driving voltages, avoids screen fading over time
    void
    hibernate() override; // turns powerOff() and sets controller to deep sleep for minimum power use, ONLY if wakeable by RST (rst >= 0)
    void setPaged(); // for GxEPD2_154c and GxEPD2_565c paged workaround
private:
    void _PowerOn();

    void _PowerOff();

    void _InitDisplay();

    void _Init_Full();

    void _Init_Part();

    void _Update_Full();

    void _Update_Part();

private:
    bool _paged;
};

#endif
